package cn.iocoder.yudao.module.lfd.controller.admin.productitem;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.lfd.controller.admin.productitem.vo.*;
import cn.iocoder.yudao.module.lfd.dal.dataobject.productitem.ProductItemDO;
import cn.iocoder.yudao.module.lfd.convert.productitem.ProductItemConvert;
import cn.iocoder.yudao.module.lfd.service.productitem.ProductItemService;

@Tag(name = "管理后台 - 理发子项")
@RestController
@RequestMapping("/lfd/product-item")
@Validated
public class ProductItemController {

    @Resource
    private ProductItemService productItemService;

    @PostMapping("/create")
    @Operation(summary = "创建理发子项")
    @PreAuthorize("@ss.hasPermission('lfd:product-item:create')")
    public CommonResult<Integer> createProductItem(@Valid @RequestBody ProductItemCreateReqVO createReqVO) {
        return success(productItemService.createProductItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新理发子项")
    @PreAuthorize("@ss.hasPermission('lfd:product-item:update')")
    public CommonResult<Boolean> updateProductItem(@Valid @RequestBody ProductItemUpdateReqVO updateReqVO) {
        productItemService.updateProductItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除理发子项")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('lfd:product-item:delete')")
    public CommonResult<Boolean> deleteProductItem(@RequestParam("id") Integer id) {
        productItemService.deleteProductItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得理发子项")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('lfd:product-item:query')")
    public CommonResult<ProductItemRespVO> getProductItem(@RequestParam("id") Long id) {
        ProductItemDO productItem = productItemService.getProductItem(id);
        return success(ProductItemConvert.INSTANCE.convert(productItem));
    }

    @GetMapping("/list")
    @Operation(summary = "获得理发子项列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('lfd:product-item:query')")
    public CommonResult<List<ProductItemRespVO>> getProductItemList(@RequestParam("ids") Collection<Integer> ids) {
        List<ProductItemDO> list = productItemService.getProductItemList(ids);
        return success(ProductItemConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得理发子项分页")
    @PreAuthorize("@ss.hasPermission('lfd:product-item:query')")
    public CommonResult<PageResult<ProductItemRespVO>> getProductItemPage(@Valid ProductItemPageReqVO pageVO) {
        PageResult<ProductItemDO> pageResult = productItemService.getProductItemPage(pageVO);
        return success(ProductItemConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出理发子项 Excel")
    @PreAuthorize("@ss.hasPermission('lfd:product-item:export')")
    @OperateLog(type = EXPORT)
    public void exportProductItemExcel(@Valid ProductItemExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ProductItemDO> list = productItemService.getProductItemList(exportReqVO);
        // 导出 Excel
        List<ProductItemExcelVO> datas = ProductItemConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "理发子项.xls", "数据", ProductItemExcelVO.class, datas);
    }

}
